/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.ejb.sessionbean;

import cassys.dataaccess.DAOException;
import cassys.dataaccess.DAOFactory;
import cassys.dataaccess.StudentDAO;
import cassys.dataaccess.TeamDAO;
import cassys.datatransferobjects.*;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateless;

/**
 *
 * @author Priyanka
 */
@Stateless(mappedName = "ResultViewBean")
public class ResultViewBean implements ResultViewBeanRemote {

    public StudentDAO dao = null;
    public StudentModuleTeamBeanRemote stdModTeamBean = new StudentModuleTeamBean();
    public ModuleBeanRemote ModTeamBean = new ModuleBean();
    public TeamDAO teamdao = null;

    public void ejbCreate() {
        dao = DAOFactory.getInstance().getStudentDAO();
        teamdao = DAOFactory.getInstance().getTeamDAO();
    }

    @Override
    public void getGPAForStudent() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public ArrayList<ModuleDTO> getAllModulesForStudent(String studentid) throws DAOException, RemoteException {
        ArrayList<StudentModuleTeamDTO> list = stdModTeamBean.findAllModuleForStudent(studentid);
        ArrayList<ModuleDTO> temp = null;
        if (list != null && list.size() > 0) {
            temp = new ArrayList<ModuleDTO>();
            Iterator<StudentModuleTeamDTO> iter = list.iterator();
            while (iter.hasNext()) {
                temp.add(ModTeamBean.findModule(iter.next().getModulecode()));
            }
        }
        return temp;
    }

    public ArrayList<ModuleDTO> getAllModulesForTeam(String teamid) throws DAOException, RemoteException {
        ArrayList<StudentModuleTeamDTO> list = stdModTeamBean.findAllModuleForTeam(teamid);
        ArrayList<ModuleDTO> temp = null;
        if (list != null && list.size() > 0) {
            temp = new ArrayList<ModuleDTO>();
            Iterator<StudentModuleTeamDTO> iter = list.iterator();
            while (iter.hasNext()) {
                temp.add(ModTeamBean.findModule(iter.next().getModulecode()));
            }
        }
        return temp;
    }

    public ArrayList<ResultDTO> getTotalSummaryForAllStudents() throws DAOException, RemoteException {
        ArrayList<ResultDTO> result = new ArrayList<ResultDTO>();

        for (StudentDTO student : dao.findAllStudent()) {

            ArrayList<ModuleDTO> modules = getAllModulesForStudent(student.getUserId());
            if (modules == null) {
                continue;
            }
            
            for (ModuleDTO module : modules) {
                System.out.println("Modules===>"+module.getModuleCode());
                ResultDTO dto = new ResultDTO();
                dto.setStudent(student.getUserId());
                dto.setStudentname(student.getStudentName());
                dto.setModuleCode(module.getModuleCode());

                Set<AssessmentDTO> assessments = module.getAssessmentList();
                if (assessments == null) {
                    continue;
                }
                float totalMark = 0;
                for (AssessmentDTO assessment : assessments) {
                    System.out.println("Assessmet===>"+assessment.getTitle());
                    List<AssessmentItemsDTO> assessmentItems = assessment.getAssessmentItems();
                    if (assessmentItems == null) {
                        continue;
                    }
                    float weightedMark = 0;
                    for (AssessmentItemsDTO item : assessmentItems) {
                        System.out.println("item===>"+item.getAssessmentItemId());
                        float evaluationMaxMark = item.getToRange() * item.getWeight();
                        float evaluationActualMark = 0;
                        
                        Set<EvaluationDTO> evaluations = assessment.getEvaluations();
                        if (evaluations == null) {
                            continue;
                        }
                        for (EvaluationDTO evaluation : evaluations) {
                            if (!evaluation.getStudentId().equals(student.getUserId()))
                                continue;
                            System.out.println("Evalutioan===>"+evaluation.getEvaluationId());
                            List<EvaluationItemDTO> evaluationItems = evaluation.getEvalutionItems();
                            if (evaluationItems == null) {
                                continue;
                            }
                            for (EvaluationItemDTO evaluationItem : evaluationItems) {
                                
                               System.out.println("Evalutioan item===>"+evaluationItem.getMark());

                                evaluationActualMark += evaluationItem.getMark() * item.getWeight();
                                dto.setEvaluationItems(assessment.getTitle(), (double) evaluationItem.getMark());
                            }
                        }

                        float percentage = evaluationActualMark / evaluationMaxMark;
                        System.out.println(percentage+" PERCENTAGE");
                        weightedMark = percentage * (float) assessment.getToRange();
                        System.out.println(weightedMark+" WEIGHT");

                    }
                    totalMark += weightedMark;

                }
                dto.setEvaluationTotal(totalMark);
                result.add(dto);
            }
        }


        return result;
    }

    public ArrayList<TeamResultDTO> getTotalSummaryForAllTeam() throws DAOException, RemoteException {

        ArrayList<TeamResultDTO> result = new ArrayList<TeamResultDTO>();
        ArrayList<TeamDTO> teamlist = teamdao.findAllTeam();
        for (int i = 0; i < teamlist.size(); i++) {
            TeamResultDTO dto = new TeamResultDTO();
            dto.setTeam_id(teamlist.get(i).getTeamId());
            dto.setTeam_name(teamlist.get(i).getDescription());
            dto.setModuleCode(teamlist.get(i).getModuleCode());
            //get all the mdoule list from studentmoduleteam            
            ArrayList<ModuleDTO> modulelist = getAllModulesForTeam(teamlist.get(i).getTeamId());

            int total = 0;
            for (int j = 0; j < modulelist.size(); j++) {
                dto.setModuleCode(modulelist.get(j).getModuleCode());
                Set<AssessmentDTO> tempAssList = modulelist.get(j).getAssessmentList();
                Iterator<AssessmentDTO> itr = tempAssList.iterator();
                while (itr.hasNext()) {
                    AssessmentDTO temp = itr.next();

                    int assessmentMaxRange = temp.getToRange();
                    float outOfPerAssessment = 0;
                    float evalMarks = 0;
                    for (int item = 0; item < temp.getAssessmentItems().size(); item++) {

                        // out of marks  
                        outOfPerAssessment = +temp.getAssessmentItem(item).getWeight() * assessmentMaxRange;


                        // actual marks scored
                        evalMarks = +temp.getAssessmentItem(item).getWeight() * temp.getEvaluationItem(i).getMark();

                    }
                    double t = ((evalMarks / outOfPerAssessment) * temp.getMaximumMark());
                    dto.setEvaluationItems(temp.getTitle(), new Double(t));
                }
            }

            dto.setEvaluationTotal(total);
            result.add(dto);
        }

        return result;



    }
}
